<template>
  <el-dialog title="提交退款申请" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="160px">
      <el-form-item label="选择企业" prop="title">
        <el-select v-model="dataForm.company_id" filterable @change="companyChange" placeholder="选择企业">
          <el-option
            v-for="item in companys"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="退款金额" prop="amount">
        <el-input v-model="dataForm.amount" placeholder="退款金额"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" :rows="4" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        companys:{},
        dataForm: {
          company_id: '',
          name: '',
          bank_number: '',
          type: 2,
          amount: '',
          remark: '',
          reason: null,
          status: 0
        },
        search: {
          title: '',
        },
        pageIndex: 1,
        pageSize: 300,
        totalPage: 0,
        dataRule: {
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl('/company/list'),
            method: 'get',
            params: this.$http.adornParams({
              'page': this.pageIndex,
              'limit': this.pageSize,
              'title': this.search.title
            })
          }).then(({data}) => {
            let _list = data.page.list
            this.companys = _list.map(function(val, idx, _list) {
              return Object.assign({}, {'id': val.company_id, 'title': val.title})
            })
            setTimeout(() => {
              this.loading = false
            }, 500);
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(!this.dataForm.company_id) {
              this.$message.error("请选择需要退款的企业")
              return
            }
            if(this.dataForm.amount <= 0) {
              this.$message.error("请输入正确的退款金额")
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/recharge/refund`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            }).catch((error) => {
              this.$message.error('请求异常')
            })
          }
        })
      }
    }
  }
</script>
